<?php
$userid = 10;
$somesecret = 'abcabcab';
$validCookie = $userid .'-' . hash('sha256', $userid . $somesecret);
// echo 'validCookie ', $validCookie;
// validCookie 10-dbdb4d162c56a34202281c492acb566add07e5949c7ff3ffcd27474235556052
// you do the setCookie stufff
// NOW COOL HACKER COMES AND CHANGES THE id to 55555555
// his new cookie will be
// 5555555555-dbdb4d162c56a34202281c492acb566add07e5949c7ff3ffcd27474235556052
// he doesnt know your somesecret
// now he visits your site with this cookie
// you validate it
$_COOKIE['test'] = '5555555555-dbdb4d162c56a34202281c492acb566add07e5949c7ff3ffcd27474235556052';
list($user, $hash) = explode('-', $_COOKIE['test']);
echo 'hello user id ', $user;
$expectedHash = hash('sha256', $user . $somesecret);
// the real check
if( $hash === $expectedHash ) echo ' you are real';
else echo ', but you are a hacker';
//now run this code
1